<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ include file="header.jsp" %>
	<style>
		.del{
		background-color:black;
		}
	</style>
 </head>
    <body>
		<div class="container">
			
			<a id="orderLink" class="easyui-linkbutton">订单号</a>
				<input id="bt" type="text" name="saleorderno" class="easyui-validatebox" style="width:344px" data-options="required:false" /><p>
				<label>菜品选择:</label><input id="cc" class="easyui-combobox" name="foodname"/><p>
				<label>名称:</label><input  id="foodname"/><p>
				<label>单价:</label><input id="price" class="easyui-validatebox" style="width:153px" data-options="required:false,validType:'Number'"/><p>
				<label>编号</label><input id="foodno" class="easyui-validatebox" data-options="required:false" /><p>
				<label>数量:</label><input id="quanlity" type="number" class="easyui-validatebox" data-options="required:false" value="1"/><p>
				<button id="add">添加</button><p>
				<label>客户：</label>
				<input id="ccc" class="easyui-combobox" name="customer"/><p>
				<label>客户编号:</label>
                <input id="customerno" class="easyui-validatebox" style="left:150px;width:75px"data-options="required:false"/>
				<input id="customer_id" type="hidden" />
				<p><label>收银员编号:</label>
				<input name="employee_no" class="easyui-validatebox" style="left:100px;width:75px" data-options="required:false"><p>
                 <label>收银时间:</label>
                 <input id="dd" name="time" class="easyui-datebox" style="width:350px" data-options="required:false"/><p>
				<label>总数量:</label>
				<input id="sumquanlity"  style="width:360px" data-options="required:false"/><p>
                <label>总金额:</label>
				<input id="sumcost" class="easyui-validatebox" style="width:360px" data-options="required:false"/><p>
				<div class="cart-payment">			
                <input type="radio" id="offline_pay_btn" name="paymentmethod" value="餐到付款" >餐到付款 
                <input type="radio" id="online_pay_btn" name="paymentmethod" value="当前支付" checked>当前支付</p>
                </div>
                  <a href="/fleshPos_kingda/showpayment.jsp" id="submit" class="easyui-linkbutton" style="width:88px;height:auto">确定</a>                        
                  <a href="/fleshPos_kingda/saleorder.jsp" class="easyui-linkbutton" style="width:88px;height:auto">返回</a><p>                       
               
               <table id="grid" ></table>
       		
       		</div>
		
		<script type="text/javascript">
	(function(){
		/**
		* 自动生成订单号
		*/
		function OrderNo(container,options){
			this.init(container,options);
		}
		OrderNo.prototype.init = function(container,options){
			this.container = container;
			this.options = {"input":"#bt"};
			this.options = $.extend(this.options,options);
			this.onEvents();
		}
		OrderNo.prototype.onEvents=function(){
			var self=this;
			$(this.container).click(function(){
				var date = new Date();
				var string = "so"+Date.parse(date);
				$(self.options.input).val(string);
				
			})
		}
		var orderNo = new OrderNo("#orderLink",{});
	})();
	
	
	$(function() {
	    loadgrid(); //加载datagrid
	/* 加载datagrid列表*/
	    function loadgrid(){
	        $('#grid').datagrid({
	        title : '菜品',
	        loadMsg : '正在加载…',  //当从远程站点载入数据时，显示的一条快捷信息。
	        fit : true,  //窗口自适应
	        nowrap: false, //设置为true，当数据长度超出列宽时将会自动截取
	        fitColumns : true, // 自动适应列宽
	        singleSelect : true, // 每次只选中一行
	        sortName : 'food_no', //默认排序字段
	        sortOrder : 'asc', // 升序asc/降序desc
	        striped : true,  // 隔行变色  
	        pagination : true,  // 在底部显示分页工具栏
	        pageNumber : 1, //初始化页码 
	        pageSize : 20,  // 指定每页的大小，服务器要加上page属性和total属性
	        pageList : [ 20, 30, 50 ], // 可以设置每页记录条数的列表，服务器要加上rows属性
	        //rownumbers : true, // 在最前面显示行号 
	        idField : 'id', // 主键属性
	        // 冻结列,当很多咧出现滚动条时该列不会动
	        frozenColumns : [ [
	        {title : '序号', width : '100', field : 'id', sortable : true}, 
	        {title : '菜品编号', width : '100', field : 'foodno', sortable : true}, 
	        {title : '菜品名', width : '100',    field : 'foodname',sortable : true}
	        ] ],
	        columns : [ [ 
	        {title : '菜品类别序号', width : '100', field : 'typeid', sortable : false}, 
	        {title : '价格（元）', width : '200',    field : 'price', sortable : false},
			{title : '数量', width : '200',    field : 'number', sortable : false},
	        {title : '菜品描述', width : '200',    field : 'description', sortable : false}
	        ] ],       
	        // 工具栏按钮
	        toolbar: [
	"-", {id: 'remove', text: '删除',iconCls: 'icon-remove', handler: function () {remove()} }, 
	        ]
	    });
	}
	    
	/* 删除数据*/
	    function remove(){
	        var row = $('#grid').datagrid('getSelected');
	        //如果没有选中记录
	        if(row == null){
	            $.messager.alert("提示", "请选择一条记录",'info');
	            return;
	        }
	        $.messager.confirm('确认', '确定要删除吗？', function (r) {
	                if (r) {
	                	$(".datagrid-row-selected").addClass("del");
	                    //提交到后台的action
	                  	delete myfoodList[row.index];
	                  	countPN();
	                    console.log(myfoodList);
	                    
	                }
	            });
	         
	    }		
	/* 刷新grid*/
	    function reload(){
	        $('#grid').datagrid('reload');
	    }
	    
	    
	});
	  //扩展easyui表单的验证  
	    $.extend($.fn.validatebox.defaults.rules, {  
	        //验证汉字  
	        CHS: {  
	            validator: function (value) {  
	                return /^[\u0391-\uFFE5]+$/.test(value);  
	            },  
	            message: 'The input Chinese characters only.'  
	        },  
	        //移动手机号码验证  
	        Mobile: {//value值为文本框中的值  
	            validator: function (value) {  
	                var reg = /^1[3|4|5|8|9]\d{9}$/;  
	                return reg.test(value);  
	            },  
	            message: '请正确的填写您的电话号码'  
	        },  
	        //国内邮编验证  
	        ZipCode: {  
	            validator: function (value) {  
	                var reg = /^[0-9]\d{5}$/;  
	                return reg.test(value);  
	            },  
	            message: 'The zip code must be 6 digits and 0 began.'  
	        },  
	      //数字  
	        Number: {  
	            validator: function (value) {  
	                var reg =/^[0-9]*\.?[0-9]*$/;  
	                return reg.test(value);  
	            },  
	            message: '请输入数字.'  
	        }
	    });
	  	var tempFood=[];
	    $("#cc").combobox({
	    	width:'200px',
	    	valueField:'id',
	        textField:'foodname',
	        url:'/fleshPos_kingda/mvc/food/list',
	        onSelect:function(res){
	        	for(var j in tempFood){
	        		tempFood.pop();
	        	}
	        	for(var i in res){
	        		tempFood[i]=res[i];
	        	}
	        	$("#foodname").val(res.foodname);
	        	$("#price").val(res.price);
	        	$("#foodno").val(res.foodno);
	        }
	       
	    });
	    $("#ccc").combobox({
	    	width:'200px',
	    	valueField:'id',
	        textField:'customername',
	        url:'/fleshPos_kingda/mvc/customer/list',
	        onSelect:function(res){
	        	$("#customerno").val(res.customerno);
	        	$("#customer_id").val(res.id);
	        }
	        	
	       
	    });
	    var dateStr ;
	    $('#dd').datebox({
    		onSelect: function(date){
    			dateStr=date.getFullYear()+":"+(date.getMonth()+1)+":"+date.getDate();
    		}
    	});
	    var myfoodList=[];
	    var index=0;
	    $("#add").click(function(){
	    	var num = $("#quanlity").val();
	    	//console.log(num);
	    	 $("#quanlity").val("1");
	    		 
	    	//叠加数量
	    	tempFood["number"]=num;
	    	tempFood["index"]=index++;
	    	myfoodList.push(tempFood.id+","+tempFood.price+","+tempFood.number+","+tempFood.index);
	    	countPN();
	    	$('#grid').datagrid('appendRow',{
	    		id:tempFood.id,
	    		foodno:tempFood.foodno,
	    		foodname:tempFood.foodname,
	    		price:tempFood.price,
	    		number:tempFood.number,
	    		index:tempFood.index
	    	});
	    	console.log(myfoodList);
	    	
	    	
	    });
	    function countPN(){
	    	var price =0;
	    	var num=0;
	    	myfoodList.forEach(function(item,index,array){
	    		var arr = item.split(',');
	    		var foodPrice = arr[1]*arr[2];
	    		price=price+foodPrice;
	    		num=num+parseInt(arr[2]);
	    	});
	    	$("#sumquanlity").val(num);
	    	$("#sumcost").val(price);
	    }
	    $("#submit").click(function(){
	    	if(myfoodList.length==0){
	    		alert("未添加数据");
	    		return false;
	    	}
	    	var data={};
	    	var cusid = $("#customer_id").val();
	    	data.foodList = myfoodList;
	    	data.customerId = cusid;
	    	data.createTime = dateStr;
	    	data.orderNo = $("#bt").val();
	    	data.total = $("#sumcost").val();
	    	var json = JSON.stringify(data);
	    	console.log(json);
	    	$.post("/fleshPos_kingda/mvc/saleorder/update",{data:json},function(data){
	    		if(data.status){
	    			alert("插入成功");
	    			window.location.href="/fleshPos_kingda/saleorder.jsp";
	    			
	    		}else{
	    			console.log(data);
	    		}
	    	},'json');
	    })
</script>
</body>
</html>